package com.poul.acfun.adapter;

import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.RequiresApi;
import android.support.design.widget.CoordinatorLayout;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewCompat;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.LinearLayoutCompat;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.GridLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.drawee.interfaces.DraweeController;
import com.facebook.drawee.view.SimpleDraweeView;
import com.poul.acfun.R;
import com.poul.acfun.ac_model.bean.PushBean;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by poul on 2016/10/20.
 */

public class PushAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context context;
    private PushBean pushBean;
    private int[] layouts;

    public PushAdapter(Context context, PushBean pushBean) {
        this.context = context;
        this.pushBean = pushBean;
        layouts = new int[]{R.layout.push_lun_bo, R.layout.item_hot_push, R.layout.item_banana,R.layout.item_fan_ju};
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = null;
        if (viewType == 0) {
            view = LayoutInflater.from(context).inflate(layouts[0], parent, false);
            return new LuboHolder(view);
        } else if (viewType == 1) {
            view = LayoutInflater.from(context).inflate(layouts[1], parent, false);
            return new HotHolder(view);
        }else if (viewType == 2){
            view = LayoutInflater.from(context).inflate(layouts[2], parent, false);
            return new BananaHolder(view);
        }else if (viewType == 3){
            view = LayoutInflater.from(context).inflate(layouts[3], parent, false);
            return new FanJuHolder(view);
        }
        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder1, int position) {
        if (holder1 instanceof LuboHolder) {
            LuboHolder holder = (LuboHolder) holder1;
            List<PushBean.DataBean.ContentsBean> contents = pushBean.getData().get(0).getContents();
            holder.lunBoImg.setAdapter(new PushFoucusAdapter(context, contents));
            for (PushBean.DataBean.ContentsBean content : contents) {
                ImageView img = new ImageView(context);
                img.setImageResource(R.drawable.circle_white);
                int i = (int) context.getResources().getDimension(R.dimen.point_padding);
                img.setPadding(i,i,i,i);
                holder.points.addView(img);
            }
            holder.lunBoImg.addOnPageChangeListener(holder);
            holder.lunBoImg.setCurrentItem(1);
            holder.point.post(new Runnable() {
                @Override
                public void run() {
                    ViewCompat.setTranslationX(holder.point,holder.points.getLeft());
                }
            });
            holder.handler.obtainMessage(0,1,0).sendToTarget();

        } else if (holder1 instanceof HotHolder) {
            HotHolder holder = (HotHolder) holder1;
            PushBean.DataBean dataBean = pushBean.getData().get(1);
            holder.barImg.setImageURI(dataBean.getImage());
            holder.text.setText(dataBean.getName());
            holder.paiHang.setImageURI(pushBean.getData().get(2).getContents().get(0).getImage());
            holder.more.setVisibility(View.GONE);
            WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
            LinearLayout linearLayout = new LinearLayout(context);
            for (int i = 0; i < dataBean.getContents().size(); i++) {
                View view = LayoutInflater.from(context).inflate(R.layout.item_hot_one, null, false);
                SimpleDraweeView simpleDraweeView = (SimpleDraweeView) view.findViewById(R.id.img_1);
                simpleDraweeView.setImageURI(dataBean.getContents().get(i).getImage());
                TextView textView = (TextView) view.findViewById(R.id.hot_title1);
                textView.setText(dataBean.getContents().get(i).getTitle());
                LinearLayout.MarginLayoutParams params = new LinearLayoutCompat.LayoutParams(windowManager.getDefaultDisplay().getWidth() / 2, ViewGroup.LayoutParams.MATCH_PARENT);
                linearLayout.addView(view,params);
                if ((i+1)%2 == 0){
                    holder.container.addView(linearLayout,new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
                    linearLayout = new LinearLayout(context);
                }
            }
        }else if (holder1 instanceof BananaHolder){
            BananaHolder holder = (BananaHolder) holder1;
            PushBean.DataBean dataBean = pushBean.getData().get(3);
            holder.bananaLogo.setImageURI(dataBean.getImage());
            holder.bananaBar.setText(dataBean.getName());
            LinearLayout linear =  new LinearLayout(context);
            for (int i = 0; i < dataBean.getContents().size(); i++) {

            }
        }else if (holder1 instanceof FanJuHolder){
            FanJuHolder holder = (FanJuHolder) holder1;
        }
    }

    @Override
    public int getItemCount() {
        return layouts.length;
    }

    @Override
    public int getItemViewType(int position) {
        Log.e("提示", "getItemViewType: ====" + position);
        if (position == 0) {
            return 0;
        } else if (position == 1) {
            return 1;
        } else if (position == 2) {
            return 2;
        } else if (position == 3){
            return 3;
        }
        return super.getItemViewType(position);
    }

    public static class LuboHolder extends RecyclerView.ViewHolder implements ViewPager.OnPageChangeListener,Handler.Callback {
        private final LinearLayout points;
        private final ImageView point;
        ViewPager lunBoImg;
        private Handler handler = new Handler(this);

        public LuboHolder(View itemView) {
            super(itemView);
            lunBoImg = (ViewPager)itemView.findViewById(R.id.push_lun_bo_img);
            points = (LinearLayout)itemView.findViewById(R.id.focus_points);
            point = (ImageView)itemView.findViewById(R.id.focus_point);
        }



        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            float value = points.getLeft() + (position - 1 + positionOffset) * point.getWidth();
            value = Math.max(value,points.getLeft());
            value = Math.min(value,points.getRight()-point.getLeft());
            ViewCompat.setTranslationX(point,value);
        }

        @Override
        public void onPageSelected(int position) {
            PagerAdapter adapter = lunBoImg.getAdapter();
            if (position == 0) {
                lunBoImg.setCurrentItem(adapter.getCount()-2,false);
            }else if (position == adapter.getCount()-1){
                lunBoImg.setCurrentItem(1,false);
            }
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }

        @Override
        public boolean handleMessage(Message msg) {
            switch (msg.what) {
                case 0:
                    int arg1 = msg.arg1;
                    if (arg1>lunBoImg.getAdapter().getCount()-2) {
                        arg1 = 1;
                    }
                    lunBoImg.setCurrentItem(arg1,arg1!=1);
                    Message message = handler.obtainMessage(0, ++arg1, 0);
                    handler.sendMessageDelayed(message,4000);
                    break;
            }
            return true;
        }
    }

    public static class HotHolder extends RecyclerView.ViewHolder {
        private final RelativeLayout more;
        private final TextView hotMore;
        private final LinearLayout container;
        SimpleDraweeView barImg;
        TextView text;
        SimpleDraweeView paiHang;


        public HotHolder(View itemView) {
            super(itemView);
            barImg = (SimpleDraweeView)itemView.findViewById(R.id.title_bar_img);
            paiHang = (SimpleDraweeView)itemView.findViewById(R.id.push_pai_hang);
            text = (TextView)itemView.findViewById(R.id.title_bar_text);
            more = (RelativeLayout)itemView.findViewById(R.id.more);
            hotMore = (TextView)itemView.findViewById(R.id.hot_more);
            container = (LinearLayout)itemView.findViewById(R.id.push_container);
        }
    }

    public static class BananaHolder extends RecyclerView.ViewHolder {

        SimpleDraweeView bananaLogo;
        TextView bananaBar;
        SimpleDraweeView bananaPaiHang;
        private final LinearLayout container;

        public BananaHolder(View itemView) {
            super(itemView);
            bananaLogo = (SimpleDraweeView)itemView.findViewById(R.id.banana_logo);
            bananaPaiHang = (SimpleDraweeView)itemView.findViewById(R.id.banana_pai_hang);
            bananaBar = (TextView)itemView.findViewById(R.id.banana_bar);
            container = (LinearLayout) itemView.findViewById(R.id.banana_container);
        }

    }

    public static class FanJuHolder extends RecyclerView.ViewHolder{

        public FanJuHolder(View itemView) {
            super(itemView);
        }
    }


}
